<?php

/**
 * View Grades is a page that allows a Student to view their grades for their selected course
 * Dependent on the student-courses.php page for selecting the course
 *
 * @Author Ryan Olson
 * @Version 1.0
 */

@session_start();
require_once 'shared-functions.php';
require_once 'session.php';
require_once 'masterpage.php';

//If the student is not logged in, redirect them to the log in page login--which will direct them to the student courses page
if (!IsValidSession()) {
    header('Location: login.php?page=view-grades');
    exit();
}
else
{
    RefreshSession();
}

$user = GetCurrentUserAccessLevel();
//Check user access
if ($user != $STUDENT) {
    header('Location: login.php?page=view-grades&error=To access to the view grades page, please log in as a student&logout=1');
    exit();
}

//If the page isn't sent an InstanceID from another page, most likely student-courses, then it redirects them to student-courses.php
if (!isset($_POST['CourseInstance'])) {
    header('Location: course-selector.php?page=view-grades&filter=student');
    exit();
}

masterpage("View Grades");

echo "<a href='course-selector.php?page=view-grades&filter=student'>&lt;&lt; Back to My Courses</a><p />";

$link = connect_db();

//Query used to get the Course information
$courses = "SELECT *
                FROM `CourseInstance`
		        INNER JOIN `Course` ON `Course`.`CourseID` = `CourseInstance`.`CourseID`
                WHERE `CourseInstance`.`InstanceID` = '" . $_POST['CourseInstance'] . "'";

$course = mysql_query($courses, $link);

//If there was an error performing the query to get the course
if (!$course) {
    echo "Ooops.. something went wrong while loading course. Please contact support. " . mysql_error();
    exit();
}
//If the course wasn't found
if (mysql_num_fields($course) == 0) {
    echo "No course found";
    exit();
}
$row = mysql_fetch_row($course);

echo "<b>Course Name:</b> " . $row[6] . "<br />";
echo "<b>Days Taught:</b> " . $row[3] . "<br />";


//Query to get the student grades for this course instance
$query = "SELECT *
		FROM `CourseInstance`
		INNER JOIN `StudentGrade` ON `StudentGrade`.`CourseInstanceID` = `CourseInstance`.`InstanceID`
		INNER JOIN `CourseWork` ON `CourseWork`.`ItemID` = `StudentGrade`.`ItemID`
		AND `CourseWork`.`CourseInstance` = `CourseInstance`.`InstanceID`
		WHERE `CourseInstance`.`InstanceID` ='" . $_POST['CourseInstance'] . "'
        AND `StudentGrade`.`StudentID` = '" . GetSessionUser() . "'";

$grades = mysql_query($query, $link);

//If there was an error performing the query for grades
if (!$grades) {
    echo "Ooops.. something went wrong while loading grades. Please contact support." . mysql_error();
    exit();
}

//If there were no grades returned
if (mysql_num_fields($grades) == 0) {
    echo "No grades for this course";
    exit();
}
?>
<br/>
<table class="DataList">
    <tr>
        <th class="header">Item Name</th>
        <th class="header">Points</th>
        <th class="header">Max Points</th>
        <th class="header">Comment</th>
    </tr>

<?php
    $rowID = 0;
    $totalMaxPoints = 0;
    $totalPoints = 0;
    if(mysql_num_rows($grades) <= 0)
    {
        echo '<tr><td class="rows" colspan="4">No Grades</td></tr>';
        echo '</table>';
    } else
    {
        //Print out the grades for the current student's course
        while ($grade = mysql_fetch_array($grades, MYSQL_BOTH))
        {
            $itemName = $grade['ItemName'];
            $points = $grade['Points'];
            $maxPoints = $grade['MaxPoints'];
            $comment = $grade['Comment'];
            $weight = $grade['Weight'];

            $totalPoints += $points * $weight;
            $totalMaxPoints += $maxPoints * $weight;

            if ($rowID == 0) {
                $rowClass = "rows";
                $rowID++;
            }
            else
            {
                $rowClass = "altrows";
                $rowID--;
            }

            echo "<tr>";
            echo "<td class='$rowClass'>$itemName</td>";
            echo "<td class='$rowClass'>$points</td>";
            echo "<td class='$rowClass'>$maxPoints</td>";
            echo "<td class='$rowClass'>$comment</td>";
            echo "</tr>";
        }
        echo '</table>';
        echo '<p />';
        echo '<table>';
        echo '<tr><td align="right"><b>Percent:</b></td><td>'.round(($totalPoints/$totalMaxPoints)*100, 2).'%</td></tr>';

        //calc percent
        echo '<tr><td align="right"><b>Grade:</b></td><td>'.GetGradeFromPercent(($totalPoints / $totalMaxPoints) * 100).'</td></tr>';
        echo '</table>';
    }


    endmasterpage();
?>

